// This file generates the panels in Figure D.5
set matsize 10000


global tables = "Tables"
global figures = "Figures"

insheet using "Data/with_header_subclass_all.csv", clear
keep main_uspto header_subclass_name header_subclass 
duplicates report main_uspto
duplicates drop main_uspto, force
save "Data/with_header_subclass_formerge.dta", replace

*** Assemble data
use "Data/control_classes_CW.dta", clear
append using "Data/control_classes_WWI.dta"
merge 1:1 patnum using "Data/all_patents_basicinfo.dta"

keep if _merge == 3
drop _merge

drop if iyear < 1840
drop if iyear > 1880 & iyear <= 1900
drop if iyear > 1940

tab iyear if nclassgoogle1 == 623




*** Generate a numeric variable for the individual war episodes
gen warepisode = .
replace warepisode = 1 if iyear >= 1840 & iyear <= 1890
replace warepisode = 2 if iyear >= 1900 & iyear <= 1940

rename iyear year

merge m:1 main_uspto using "Data/with_header_subclass_formerge.dta"


tab _merge
drop if _merge == 1
drop if _merge == 2
drop _merge

merge m:1 patnum using "Data/Prosthetics_adjust_names.dta"
drop _merge




*gen nclassgoogle2digit1 = substr(nclassgoogle2,1,1) 
*egen main_usptonum = group(nclassgoogle1 nclassgoogle2digit1)
gen patentcount = 1
gen prosthetics = nclassgoogle1 == 623  

save "Data/CivilWarIntemediateData_limb_difference.dta", replace


local limb "arm"

local trait_interest "cost"


use "Data/CivilWarIntemediateData_limb_difference.dta", clear
// Set limb to "arm" to generate arm results or to "leg" to generate leg results

// Set trait_interest to "cost" for cost results for either arm or legs and to "production" for the production aggregate trait for both
// limb = "leg"
// local trait_interest = "cost"

if "`limb'" == "arm" {
drop if leg == 1 // for arms
}
else{
drop if leg == 0 //For legs
}

gen production = (adjustability + simplicity + cost + materials)/4 
gen anyproduction = max(adjustability, simplicity, cost, materials) 
if "`trait_interest'" == "production" {
drop cost //If generating production variable
gen cost = production  //If generating production variable
}



drop if year < 1850 | year > 1870

collapse (sum) patentcount (mean) cost, by(nclassgoogle1 year)

tsset nclassgoogle1 year
tsfill, full

foreach var in cost patentcount {
	replace `var' = 0 if `var' == .
}

foreach var in cost {
	gen threeyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount)/(patentcount + L.patentcount + L2.patentcount)
}

foreach var in cost {
	gen fiveyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount + L4.`var'*L4.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount + L4.patentcount)
}

foreach var in cost {
	gen fouryear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount)
}

foreach var in cost {
	gen sixyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount + L4.`var'*L4.patentcount + L5.`var'*L5.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount + L4.patentcount + L5.patentcount)
}

drop if year < 1851

egen basepatentcostB = total(cost) if year >= 1855 & year <= 1861 , by(nclassgoogle1)
egen basepatentcost = max(basepatentcostB), by(nclassgoogle1)


if "`limb'" == "leg" {
drop if basepatentcost == 0 //For legs only for better match
}



if "`limb'" == "leg" {
gen missingkeyvariable = fouryearcost == . if year >= 1855 //For legs only for better match
}
else{
gen missingkeyvariable = fouryearcost == . if year >= 1856 //for arms

}
egen anymissing = max(missingkeyvariable), by(nclassgoogle1)
drop if anymissing == 1

gen L5fouryearcost = L5.fouryearcost
gen D5fouryearcost = fouryearcost - L5fouryearcost

if "`limb'" == "leg" {
drop if year < 1855 //for legs
}
else{
drop if year < 1856 //for arms

}

local filename "Data/SyntheticControlCIV`trait_interest'_`limb'.dta"

if "`limb'" == "leg" {
synth fouryearcost fouryearcost(1861) fouryearcost(1860) fouryearcost(1859) fouryearcost(1858) fouryearcost(1857) fouryearcost(1856) fouryearcost(1855), trunit(623) trperiod(1862) keep(`filename') replace

}
else{
synth fouryearcost fouryearcost(1862) fouryearcost(1861) fouryearcost(1860) fouryearcost(1859) fouryearcost(1858) fouryearcost(1857) fouryearcost(1856), trunit(623) trperiod(1863) keep(`filename') replace //for arms, use 1863 for closer match

}






use `filename', clear

rename _time year
rename _Y_synthetic fouryearcostcontrol
rename _Y_treated  fouryearcosttreatment
tsset year 

local filename2 "Figures/Synth`trait_interest'CIV_`limb'.pdf"


twoway (tsline fouryearcosttreatment if year >= 1855 & year <= 1870, xline(1862, lcolor(black) lpattern(dash)) xline(1867, lcolor(black) lpattern(dash)) lwidth(medthick)) (tsline fouryearcostcontrol if year >= 1855 & year <= 1870, lpattern(dash) lwidth(medthick)), ytitle(Patent Share) title(Patent Share by Year: cost, color(black)) xlabel(1855(5)1870) ylabel(0(.2).8) legend(label(1 "Prosthetics") label(2 "Synthetic Control")) graphregion(fcolor(white)) scale(0.9) name(costShareCIVSynth, replace) 
graph export `filename2', replace name(costShareCIVSynth)



local limb "arm"

local trait_interest "production"


use "Data/CivilWarIntemediateData_limb_difference.dta", clear
// Set limb to "arm" to generate arm results or to "leg" to generate leg results

// Set trait_interest to "cost" for cost results for either arm or legs and to "production" for the production aggregate trait for both
// limb = "leg"
// local trait_interest = "cost"

if "`limb'" == "arm" {
drop if leg == 1 // for arms
}
else{
drop if leg == 0 //For legs
}

gen production = (adjustability + simplicity + cost + materials)/4 
gen anyproduction = max(adjustability, simplicity, cost, materials) 
if "`trait_interest'" == "production" {
drop cost //If generating production variable
gen cost = production  //If generating production variable
}



drop if year < 1850 | year > 1870

collapse (sum) patentcount (mean) cost, by(nclassgoogle1 year)

tsset nclassgoogle1 year
tsfill, full

foreach var in cost patentcount {
	replace `var' = 0 if `var' == .
}

foreach var in cost {
	gen threeyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount)/(patentcount + L.patentcount + L2.patentcount)
}

foreach var in cost {
	gen fiveyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount + L4.`var'*L4.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount + L4.patentcount)
}

foreach var in cost {
	gen fouryear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount)
}

foreach var in cost {
	gen sixyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount + L4.`var'*L4.patentcount + L5.`var'*L5.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount + L4.patentcount + L5.patentcount)
}

drop if year < 1851

egen basepatentcostB = total(cost) if year >= 1855 & year <= 1861 , by(nclassgoogle1)
egen basepatentcost = max(basepatentcostB), by(nclassgoogle1)


if "`limb'" == "leg" {
drop if basepatentcost == 0 //For legs only for better match
}



if "`limb'" == "leg" {
gen missingkeyvariable = fouryearcost == . if year >= 1855 //For legs only for better match
}
else{
gen missingkeyvariable = fouryearcost == . if year >= 1856 //for arms

}
egen anymissing = max(missingkeyvariable), by(nclassgoogle1)
drop if anymissing == 1

gen L5fouryearcost = L5.fouryearcost
gen D5fouryearcost = fouryearcost - L5fouryearcost

if "`limb'" == "leg" {
drop if year < 1855 //for legs
}
else{
drop if year < 1856 //for arms

}

local filename "Data/SyntheticControlCIV`trait_interest'_`limb'.dta"

if "`limb'" == "leg" {
synth fouryearcost fouryearcost(1861) fouryearcost(1860) fouryearcost(1859) fouryearcost(1858) fouryearcost(1857) fouryearcost(1856) fouryearcost(1855), trunit(623) trperiod(1862) keep(`filename') replace

}
else{
synth fouryearcost fouryearcost(1862) fouryearcost(1861) fouryearcost(1860) fouryearcost(1859) fouryearcost(1858) fouryearcost(1857) fouryearcost(1856), trunit(623) trperiod(1863) keep(`filename') replace //for arms, use 1863 for closer match

}






use `filename', clear

rename _time year
rename _Y_synthetic fouryearcostcontrol
rename _Y_treated  fouryearcosttreatment
tsset year 

local filename2 "Figures/Synth`trait_interest'CIV_`limb'.pdf"


twoway (tsline fouryearcosttreatment if year >= 1855 & year <= 1870, xline(1862, lcolor(black) lpattern(dash)) xline(1867, lcolor(black) lpattern(dash)) lwidth(medthick)) (tsline fouryearcostcontrol if year >= 1855 & year <= 1870, lpattern(dash) lwidth(medthick)), ytitle(Patent Share) title(Patent Share by Year: cost, color(black)) xlabel(1855(5)1870) ylabel(0(.2).8) legend(label(1 "Prosthetics") label(2 "Synthetic Control")) graphregion(fcolor(white)) scale(0.9) name(costShareCIVSynth, replace) 
graph export `filename2', replace name(costShareCIVSynth)


local limb "leg"

local trait_interest "cost"


use "Data/CivilWarIntemediateData_limb_difference.dta", clear
// Set limb to "arm" to generate arm results or to "leg" to generate leg results

// Set trait_interest to "cost" for cost results for either arm or legs and to "production" for the production aggregate trait for both
// limb = "leg"
// local trait_interest = "cost"

if "`limb'" == "arm" {
drop if leg == 1 // for arms
}
else{
drop if leg == 0 //For legs
}

gen production = (adjustability + simplicity + cost + materials)/4 
gen anyproduction = max(adjustability, simplicity, cost, materials) 
if "`trait_interest'" == "production" {
drop cost //If generating production variable
gen cost = production  //If generating production variable
}



drop if year < 1850 | year > 1870

collapse (sum) patentcount (mean) cost, by(nclassgoogle1 year)

tsset nclassgoogle1 year
tsfill, full

foreach var in cost patentcount {
	replace `var' = 0 if `var' == .
}

foreach var in cost {
	gen threeyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount)/(patentcount + L.patentcount + L2.patentcount)
}

foreach var in cost {
	gen fiveyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount + L4.`var'*L4.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount + L4.patentcount)
}

foreach var in cost {
	gen fouryear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount)
}

foreach var in cost {
	gen sixyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount + L4.`var'*L4.patentcount + L5.`var'*L5.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount + L4.patentcount + L5.patentcount)
}

drop if year < 1851

egen basepatentcostB = total(cost) if year >= 1855 & year <= 1861 , by(nclassgoogle1)
egen basepatentcost = max(basepatentcostB), by(nclassgoogle1)


if "`limb'" == "leg" {
drop if basepatentcost == 0 //For legs only for better match
}



if "`limb'" == "leg" {
gen missingkeyvariable = fouryearcost == . if year >= 1855 //For legs only for better match
}
else{
gen missingkeyvariable = fouryearcost == . if year >= 1856 //for arms

}
egen anymissing = max(missingkeyvariable), by(nclassgoogle1)
drop if anymissing == 1

gen L5fouryearcost = L5.fouryearcost
gen D5fouryearcost = fouryearcost - L5fouryearcost

if "`limb'" == "leg" {
drop if year < 1855 //for legs
}
else{
drop if year < 1856 //for arms

}

local filename "Data/SyntheticControlCIV`trait_interest'_`limb'.dta"

if "`limb'" == "leg" {
synth fouryearcost fouryearcost(1861) fouryearcost(1860) fouryearcost(1859) fouryearcost(1858) fouryearcost(1857) fouryearcost(1856) fouryearcost(1855), trunit(623) trperiod(1862) keep(`filename') replace

}
else{
synth fouryearcost fouryearcost(1862) fouryearcost(1861) fouryearcost(1860) fouryearcost(1859) fouryearcost(1858) fouryearcost(1857) fouryearcost(1856), trunit(623) trperiod(1863) keep(`filename') replace //for arms, use 1863 for closer match

}






use `filename', clear

rename _time year
rename _Y_synthetic fouryearcostcontrol
rename _Y_treated  fouryearcosttreatment
tsset year 

local filename2 "Figures/Synth`trait_interest'CIV_`limb'.pdf"


twoway (tsline fouryearcosttreatment if year >= 1855 & year <= 1870, xline(1862, lcolor(black) lpattern(dash)) xline(1867, lcolor(black) lpattern(dash)) lwidth(medthick)) (tsline fouryearcostcontrol if year >= 1855 & year <= 1870, lpattern(dash) lwidth(medthick)), ytitle(Patent Share) title(Patent Share by Year: cost, color(black)) xlabel(1855(5)1870) ylabel(0(.2).8) legend(label(1 "Prosthetics") label(2 "Synthetic Control")) graphregion(fcolor(white)) scale(0.9) name(costShareCIVSynth, replace) 
graph export `filename2', replace name(costShareCIVSynth)



local limb "leg"

local trait_interest "production"


use "Data/CivilWarIntemediateData_limb_difference.dta", clear
// Set limb to "arm" to generate arm results or to "leg" to generate leg results

// Set trait_interest to "cost" for cost results for either arm or legs and to "production" for the production aggregate trait for both
// limb = "leg"
// local trait_interest = "cost"

if "`limb'" == "arm" {
drop if leg == 1 // for arms
}
else{
drop if leg == 0 //For legs
}

gen production = (adjustability + simplicity + cost + materials)/4 
gen anyproduction = max(adjustability, simplicity, cost, materials) 
if "`trait_interest'" == "production" {
drop cost //If generating production variable
gen cost = production  //If generating production variable
}



drop if year < 1850 | year > 1870

collapse (sum) patentcount (mean) cost, by(nclassgoogle1 year)

tsset nclassgoogle1 year
tsfill, full

foreach var in cost patentcount {
	replace `var' = 0 if `var' == .
}

foreach var in cost {
	gen threeyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount)/(patentcount + L.patentcount + L2.patentcount)
}

foreach var in cost {
	gen fiveyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount + L4.`var'*L4.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount + L4.patentcount)
}

foreach var in cost {
	gen fouryear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount)
}

foreach var in cost {
	gen sixyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount + L4.`var'*L4.patentcount + L5.`var'*L5.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount + L4.patentcount + L5.patentcount)
}

drop if year < 1851

egen basepatentcostB = total(cost) if year >= 1855 & year <= 1861 , by(nclassgoogle1)
egen basepatentcost = max(basepatentcostB), by(nclassgoogle1)


if "`limb'" == "leg" {
drop if basepatentcost == 0 //For legs only for better match
}



if "`limb'" == "leg" {
gen missingkeyvariable = fouryearcost == . if year >= 1855 //For legs only for better match
}
else{
gen missingkeyvariable = fouryearcost == . if year >= 1856 //for arms

}
egen anymissing = max(missingkeyvariable), by(nclassgoogle1)
drop if anymissing == 1

gen L5fouryearcost = L5.fouryearcost
gen D5fouryearcost = fouryearcost - L5fouryearcost

if "`limb'" == "leg" {
drop if year < 1855 //for legs
}
else{
drop if year < 1856 //for arms

}

local filename "Data/SyntheticControlCIV`trait_interest'_`limb'.dta"

if "`limb'" == "leg" {
synth fouryearcost fouryearcost(1861) fouryearcost(1860) fouryearcost(1859) fouryearcost(1858) fouryearcost(1857) fouryearcost(1856) fouryearcost(1855), trunit(623) trperiod(1862) keep(`filename') replace

}
else{
synth fouryearcost fouryearcost(1862) fouryearcost(1861) fouryearcost(1860) fouryearcost(1859) fouryearcost(1858) fouryearcost(1857) fouryearcost(1856), trunit(623) trperiod(1863) keep(`filename') replace //for arms, use 1863 for closer match

}






use `filename', clear

rename _time year
rename _Y_synthetic fouryearcostcontrol
rename _Y_treated  fouryearcosttreatment
tsset year 

local filename2 "Figures/Synth`trait_interest'CIV_`limb'.pdf"


twoway (tsline fouryearcosttreatment if year >= 1855 & year <= 1870, xline(1862, lcolor(black) lpattern(dash)) xline(1867, lcolor(black) lpattern(dash)) lwidth(medthick)) (tsline fouryearcostcontrol if year >= 1855 & year <= 1870, lpattern(dash) lwidth(medthick)), ytitle(Patent Share) title(Patent Share by Year: cost, color(black)) xlabel(1855(5)1870) ylabel(0(.2).8) legend(label(1 "Prosthetics") label(2 "Synthetic Control")) graphregion(fcolor(white)) scale(0.9) name(costShareCIVSynth, replace) 
graph export `filename2', replace name(costShareCIVSynth)

